import { QF } from '@dm/http_request';
import { message } from 'antd';

export default function CreateFetch(prePath: string | undefined) {
  const RFetch = new QF(prePath);
  RFetch.afterFetch = (data, res) => {
    const resData = data;
    // 跳转登录
    if (resData && (resData.code === 302 || resData.code === '302' || resData.code === 303 || resData.code === '303')) {
      window.history.pushState(null, '', '/');
      throw new Error(resData.message);
    }
    if ((resData && resData.code === 401) || resData.code === '401') {
      message.error(data.msg);
      window.location.href = '/login';
      throw new Error(data.msg);
    }
    return Promise.resolve();
  };
  // 重置head参数
  RFetch.setHeads = (fromHead) => {
    const headers = {
      ...fromHead,
    };
    return Promise.resolve(headers);
  };
  return RFetch;
}

export const CreateOpsWebApp = () => CreateFetch(`/api`);
